package face.yuque;

import java.util.Arrays;

/**
 * @author spf
 * @date 2024/12/12
 * @time 21:12
 */

//两数之和
//方法一：双指针
public class TwoSum4 {

    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4, 5, 6};
        int target = 10;
        System.out.println(Arrays.toString(twoSum(nums, target)));
    }

    private static int[] twoSum(int[] nums, int target) {
        int low = 0, high = nums.length - 1;
        while (low < high) {
            int sum = nums[low] + nums[high];
            if (sum == target) {
                return new int[]{low, high};
            } else if (sum > target) {
                high--;
            } else {
                low++;
            }
        }
        return new int[]{0};
    }

}
